<?php
// get_posts.php
session_start(); // 开始会话

// 检查用户是否登录
if (!isset($_SESSION['user_id'])) {
    die(json_encode(['error' => '用户未登录']));
}

// 数据库连接信息
$dbHost = 'localhost';
$dbName = 'db';
$dbUser = 'root';
$dbPass = '';

// 创建数据库连接
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);

// 检查连接
if ($conn->connect_error) {
    die(json_encode(['error' => '数据库连接失败']));
}

// get_posts.php
// 数据库连接代码...

// 获取所有公开动态
$sql = "SELECT p.id, p.content, p.image_url, p.created_at, u.username FROM posts p JOIN users u ON p.user_id = u.id ORDER BY p.created_at DESC";
$result = $conn->query($sql);

$posts = [];
while ($row = $result->fetch_assoc()) {
    $posts[] = $row;
}

// 返回JSON
header('Content-Type: application/json');
echo json_encode($posts);
?>